/**
 * 将菜品根据荤菜、素菜、主食依次排序
 *
 * @param foodItems
 */
export const sortFoods = (foodItems) => {
  const foods = []

  filterFoods(foodItems, 1).forEach(ele => foods.push(ele))
  filterFoods(foodItems, 2).forEach(ele => foods.push(ele))
  filterFoods(foodItems, 3).forEach(ele => foods.push(ele))
  return foods
};

/**
 * 筛选指定类型的菜品
 *
 * @param foodItems
 * @param type
 * @returns {*[]}
 */
export const filterFoods = (foodItems, type) => {
  const foods = []
  for (let i = 0; i < foodItems.length; i++) {
    const food = foodItems[i]

    if (type === 1 && food.foodCategoryOrderLimit === 1) {
      foods.push(food)
    }
    if (type === 2 && (food.foodCategoryOrderLimit === 2 && food.foodCategoryName !== '主食')) {
      foods.push(food)
    }
    if (type === 3 && (food.foodCategoryOrderLimit === 2 && food.foodCategoryName === '主食')) {
      foods.push(food)
    }
  }

  foods.sort(function (a, b) {
    return a.name.length - b.name.length
  })
  return foods
};

/**
 * 计算菜品类型的数量
 *
 * @param foods
 * @param foodItems
 * @param type
 * @returns {*[]}
 */
export const computeFoodsCount = (foods = [], foodItems, type) => {
  for (let i = 0; i < foodItems.length; i++) {
    const food = foodItems[i]
    const foodIds = foods.map(it => it.id)

    if (type === 1 && food.foodCategoryOrderLimit !== 1) {
      continue
    }
    if (type === 2 && (food.foodCategoryOrderLimit !== 2 || food.foodCategoryName === '主食')) {
      continue
    }
    if (type === 3 && (food.foodCategoryOrderLimit !== 2 || food.foodCategoryName !== '主食')) {
      continue
    }
    if (foodIds.includes(food.id)) {
      foods.forEach(it => {
        if (it.id === food.id) {
          it.count += 1
        }
      })
    } else {
      foods.push({
        id: food.id,
        name: food.name,
        count: 1
      })
    }
  }
  return foods
};
